package dao

import (
	"example_shorten/model"
)

func CreateUrl(url *model.Url) error {
	return SqlSession.Create(url).Error
}

func CreateUrls(urls []*model.Url) error {
	return SqlSession.Create(urls).Error
}

func DeleteUrl(urlId int64) error {
	return SqlSession.Where("id = ?", urlId).Delete(&model.Url{}).Error
}

func UpdateUrl(url *model.Url) error {
	return SqlSession.Model(&model.Url{}).Where("long_url = ?", url.LongUrl).Updates(url).Error
}

func QueryUrlByLongUrl(longUrl string) (*model.Url, error) {
	var res model.Url
	return &res, SqlSession.Where("long_url = ?", longUrl).First(&res).Error
}

func QueryUrlByShortUrl(shortUrl string) (*model.Url, error) {
	var res model.Url
	return &res, SqlSession.Where("short_url = ?", shortUrl).First(&res).Error
}
